/*
 * trans_s.h
 *
 * Created: 2012-07-13 11:23:53
 *  Author: Antrykot
 */ 



////////////////////// TRANS //////////////////////
void trans_k();
u8 trans_p();

u8 crc();

extern u8 empty_buffer;

extern u8 timeout;
extern u8 t0_cnt;

extern u16 debug_data;

#define T0START	TCCR0B = 0 | ( 1 << CS01) 		// T0, prescale 8
#define T0STOP	TCCR0B = 0 		// T0, stopped

#define TR_K_ENABLE EIMSK |= (1 << INT0)
#define TR_K_DISABLE EIMSK &= ~(1 << INT0)

//#define TR_P_ENABLE EIMSK |= (1 << INT1)
//#define TR_P_DISABLE EIMSK &= ~(1 << INT1)

#define KTX_LOW	PORTD &= ~(1 << 1)		// pd1 tx - k
#define KTX_HIG	PORTD |= (1 << 1)		// pd1 tx - k
#define KRX (PIND & (1 << 0)) 			// pd1 rx - k
#define KCK ((PIND & (1 << 2)) >> 2) 			// pd1 clk - k

#define PTX_LOW	PORTD &= ~(1 << 5)		// pd1 tx - p
#define PTX_HIG	PORTD |= (1 << 5)		// pd1 tx - p
#define PRX ((PIND & (1 << 4)) >> 4) 	// pd1 rx - p
#define PRXX PIND & (1 << 4)

#define PCK_L PORTD &= ~(1 << 3);
#define PCK_H PORTD |= (1 << 3);

//#define PCK_L DDRD |=  (1 << 3);
//#define PCK_H DDRD &= ~(1 << 3);

extern u8 rk_addr;
extern u16 rk_data;
extern u8 rk_crc;

extern u8 tk_addr;
extern u16 tk_data;
extern u8 tk_crc;
extern u16 tk_pom;
extern u8 tk_repeat;

extern u8 tp_addr;
extern u16 tp_data;
extern u8 tp_crc;
extern u8 tp_repeat;

extern u8 rp_addr;
extern u16 rp_data;
extern u8 rp_crc;
extern u16 rp_pom;

#if defined(MEDIA_PS) || defined(MEDIA_PD)
extern u16 pom_rh;
extern u16 pom_pr;
extern u16 pom_tm;
#endif

extern u8 tk;
extern u8 tp;

extern u8 crc_data[10];
extern u8 crc_data_len;

extern u8 trp_ok;

extern u8 kal_f;

extern u8 ksp_error;

extern u16 last_time;		// last time there were button pushed in kalib

void w_k_clk();
void w_k_clk();
u8 crc();
u8 get_8b_k();
void set_8b_k(u8 data);
void add_crc8(u8 byte);
void add_crc16(u16 word);


void interpret_sp();
void interpret_sk();

void wait_deliver_P();



extern u16 time_cnt_h;
extern u16 time_cnt;

#define ADDR_VAKU	0x50
#define ADDR_VPOW	0x51
#define ADDR_VDC	0x52
#define ADDR_TEMP	0x53

extern u8 p_ack;
extern u8 k_ack;


#define P_ADDR_SYNC						0x50


#define P_ADDR_RESET_DEFAULTS			0x25
#define P_ADDR_RESET_FACT_DEFAULTS		0x26

#define P_ADDR_SW_HRANGE				0x10
#define P_ADDR_SW_HRANGEUSER			0x11
#define P_ADDR_SW_LRANGE				0x12

#define P_ADDR_CALHZ					0x61
#define P_ADDR_CALHS					0x62
#define P_ADDR_CALLZ					0x63
#define P_ADDR_CALLS					0x64

#define P_ADDR_SCALHZ					0x73
#define P_ADDR_SCALHS1					0x74
#define P_ADDR_SCALHS2					0x75
#define P_ADDR_SCALLZ					0x71
#define P_ADDR_SCALLS					0x72
#define P_ADDR_PR_ZERO					0x76

#define P_ADDR_INCAKU					0x30
#define P_ADDR_DECAKU					0x31
#define P_ADDR_INCPOW					0x32
#define P_ADDR_DECPOW					0x33
#define P_ADDR_INCAKUM					0x34
#define P_ADDR_DECAKUM					0x35
#define P_ADDR_INCPOWM					0x36
#define P_ADDR_DECPOWM					0x37


#define P_ADDR_L_RESET					0x80
#define P_ADDR_H_RESET					0x81
#define P_ADDR_IR_INIT					0x85

#define P_ADDR_L_FACTORY				0x82
#define P_ADDR_H_FACTORY				0x83

#define P_ADDR_DCON						0x02
#define P_ADDR_DCOFF					0x03


#define P_ADDR_GETREF					0xE1
#define P_ADDR_GETACT					0xE2
#define P_ADDR_GET_TCAL					0xE3
#define P_ADDR_GET_BETA					0xE4
#define P_ADDR_INCR_BETA				0xE5
#define P_ADDR_DECR_BETA				0xE6


#define P_ADDR_VERSION					0x40

#define K_ADDR_VERSION					0x40

#define K_ADDR_SYNC						0x50


#define K_ADDR_CALFP					0x90
#define K_ADDR_CALFM					0x91

#define K_ADDR_HZ						0x61
#define K_ADDR_HS						0x62
#define K_ADDR_LZ						0x63
#define K_ADDR_LS						0x64

#define K_ADDR_CAL_START				0x20
#define K_ADDR_CAL_FINISH				0x21
#define K_ADDR_CAL_STOP					0x22


#define K_ADDR_ACCEPT					0x68
#define K_ADDR_DENY						0x69

#define K_ADDR_SERWIS					0x6E


#define K_ADDR_NR						0x30
#define K_ADDR_DATE						0x35


#define K_ADDR_DEBUG					0xA0
#define K_ADDR_DEBUG_AL					0xA1

#define K_ADDR_ALARMS					0xB0
#define K_ADDR_INPUT					0xB4

#define K_ADDR_KSP						0xC0

#define K_ADDR_RS_ASKNUM				0xD0


#define K_ADDR_LRANGE					0xC2
#define K_ADDR_HRANGE					0xC4
#define K_ADDR_PARAM					0xC6


#define K_ADDR_NO_COMM					0xBE

#define K_ADDR_BATT_VOLT				0xD8
#define K_ADDR_LINE_VOLT				0xD9
#define K_ADDR_LINE_LOAD				0xDA
#define K_ADDR_PELI_VOLT				0xDB

#define EMPTY_BUFFER					0xFFFF